package com.xscha.tableforge.service.impl;

import com.xscha.soyue.common.core.utils.BeanMapperUtils;
import com.xscha.tableforge.model.entity.NodeMeta;
import com.xscha.tableforge.model.vo.NodeMetaVo;
import com.xscha.tableforge.service.NodeService;
import com.xscha.tableforge.repository.NodeMetaRepository;
import com.xscha.tableforge.utils.IdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {

    @Autowired
    private NodeMetaRepository nodeMetaRepository;

    @Override
    public List<NodeMetaVo> getAppNodes(String appId) {
        List<NodeMeta> entities = nodeMetaRepository.findByAppId(appId);
        return BeanMapperUtils.mapAsList(entities, NodeMetaVo.class);
    }

    @Override
    @Transactional
    public NodeMetaVo createNode(NodeMetaVo nodeVo) {
        NodeMeta entity = BeanMapperUtils.map(nodeVo, NodeMeta.class);
		entity.setId(IdUtil.createNodeId());
        entity = nodeMetaRepository.save(entity);
        return BeanMapperUtils.map(entity, NodeMetaVo.class);
    }

    @Override
    @Transactional
    public NodeMetaVo updateNode(NodeMetaVo nodeVo) {
        NodeMeta entity = BeanMapperUtils.map(nodeVo, NodeMeta.class);
        entity = nodeMetaRepository.update(entity);
        return BeanMapperUtils.map(entity, NodeMetaVo.class);
    }

    @Override
    @Transactional
    public Boolean deleteNode(Long id) {
        return nodeMetaRepository.delete(id);
    }

    @Override
    @Transactional
    public Boolean moveNode(Long id, String parentId, Double displayOrder) {
        return nodeMetaRepository.move(id, parentId, displayOrder);
    }
} 